<template>
  <el-button v-if="editStatus" type="text" size="mini" @click.stop="handleSeriesToTop(1)">置顶</el-button>
  <el-button v-else type="text" size="mini" :disabled="true">置顶</el-button>
  <el-button v-if="seriesInfo.topno > 1 && editStatus" type="text" size="mini" @click.stop="handleSeriesToTop(0)">取消置顶</el-button>
  <el-button v-else type="text" size="mini" :disabled="true">取消置顶</el-button>
</template>

<script>
  import { toRef } from 'vue'
  import {ElMessage, ElMessageBox} from 'element-plus'
  import {seriesToTop} from "@/api/series/series";

  export default {
    name: "SeriesResort",
    props: {
      row: {
        type: Object,
        required: true
      },
      editStatus: {
        type: Boolean,
        default: true
      }
    },
    emits: ['reloadSeries'],
    setup(props, context) {
      const seriesInfo = toRef(props, 'row')
      const reloadSeriesList = () => {
        context.emit('reloadSeries')
      }
      const handleSeriesToTop = (action) => {
        let confirmMsg = action == 1 ? '确定将 ' + props.row.name + ' 进行置顶吗？' : '确定将 ' + props.row.name + ' 取消置顶吗？'
        ElMessageBox.confirm(
            confirmMsg,
            '媒资视频置顶',
            {
                confirmButtonText: '确定',
                cancelButtonText: '取消',
                type: 'warning'
            }
        ).then(() => {
          seriesToTop({mediaid: props.row.mediaid, seriesid: props.row.seriesid, iftop: action}).then(response => {
            if (response.errcode == 0) {
              ElMessage({type: 'success', message: '操作成功', duration: 2000})
              reloadSeriesList()
            }else{
              console.log(response)
            }
          })
        }).catch((err) => {
            console.log(err)
        })
      }
      return {
        seriesInfo,
        handleSeriesToTop
      }
    }
  }
</script>

<style scoped>

</style>
